//引入必要模块
const express = require('express');
const mysql = require('../mysql');

//建立路由组
const router = express.Router();

//用户登录
router.get('/login/:phone&:upwd',(req,res)=>{
    let body = req.params;
    //执行sql命令
    mysql.query('select * from sh_user where phone=? and upwd=? and is_del=0',[body.phone,body.upwd],(err,result)=>{
        if(err) throw err;
        // console.log(result.length);
        if(result.length != 0){
            return res.send(result);
        }
        res.send('0');
    });
});

//电话判断是否被注册
router.get('/getUname/:uname',(req,res)=>{
    let phone = req.params.uname;

    mysql.query('select * from sh_user where phone=?',[phone],(err,result)=>{
        if(err) throw err;
        if(result.length != 0){
            return res.send('1');
        }
        res.send('0');
    });
});

//用户注册 
router.post('/reg',(req,res)=>{
    let body = req.body;
    // console.log(body);

    mysql.query('insert into sh_user set ?',[body],(err,result)=>{
        if(err) throw err;
        if(result.affectedRows != 0){
            return res.send('1');
        }
        res.send('0');
    });
});

//根据id获取用户数据
router.get('/getUserById/:uid',(req,res)=>{
    let body = req.params;
    //执行sql命令
    mysql.query('select * from sh_user where uid=?',[body.uid],(err,result)=>{
        if(err) throw err;
        if(result.length != 0){
            return res.send(result);
        }
        res.send('0');
    });
});

// 根据id用户更新，用户修改
router.post('/update',(req,res)=>{
    let body = req.body;
    // console.log(body);
    
    mysql.query('update sh_user set ? where uid=?',[body,body.uid],(err,result)=>{
        if(err) throw err;
        if(result.affectedRows!=0){
            return res.send('1');
        }
        res.send('0');
    });
});

//添加地址
router.post('/address',(req,res)=>{
    let body = req.body;
    //执行sql命令
    mysql.query('insert into sh_address set ?',[body],(err,result)=>{
        if(err) throw err;
        if(result.affectedRows != 0){
            return res.send('1');
        }
        res.send('0');
    });
});

//获取地址
router.get('/get_address/:uid',(req,res)=>{
    let body = req.params;
    //执行sql命令
    mysql.query('select * from sh_address where uid=?',[body.uid],(err,result)=>{
        if(err) throw err;
        if(result.length != 0){
            return res.send(result);
        }
        res.send('0');
    });
});

//根据地址id获取地址
router.get('/addressById/:aid',(req,res)=>{
    let body = req.params;
    //执行sql命令
    mysql.query('select * from sh_address where aid=?',[body.aid],(err,result)=>{
        if(err) throw err;
        if(result.length != 0){
            return res.send(result);
        }
        res.send('0');
    });
});

//删除地址
router.get('/del_address/:uid',(req,res)=>{
    let body = req.params;
    //执行sql命令
    mysql.query('delete from sh_address where uid=?',[body.uid],(err,result)=>{
        if(err) throw err;
        if(result.affectedRows!=0){
            return res.send('1');
        }
        res.send('0');
    });
});

//添加订单
router.post('/order',(req,res)=>{
    let body = req.body;
    //执行sql命令
    mysql.query('insert into sh_order set ?',[body],(err,result)=>{
        if(err) throw err;
        if(result.affectedRows != 0){
            return res.send('1');
        }
        res.send('0');
    });
});

//更新订单状态
router.post('/order_update',(req,res)=>{
    let body = req.body;
    // console.log(body);

    mysql.query('update sh_order set ? where orderTime=? and uid=?',[body,body.orderTime,body.uid],(err,result)=>{
        if(err) throw err;
        if(result.affectedRows!=0){
            return res.send('1');
        }
        res.send('0');
    });
});
//更新订单状态
router.post('/order_updateByOid',(req,res)=>{
    let body = req.body;
    // console.log(body);

    mysql.query('update sh_order set ? where oid=?',[body,body.oid],(err,result)=>{
        if(err) throw err;
        if(result.affectedRows!=0){
            return res.send('1');
        }
        res.send('0');
    });
});

// 根据id查找订单数据
router.get('/get_order/:uid',(req,res)=>{
    let body = req.params;
    let sql = 'select sh_order.*,sh_product.* from sh_product inner join sh_order on sh_order.pid = sh_product.pid where sh_order.uid=?';
    //执行sql命令
    mysql.query(sql,[body.uid],(err,result)=>{
        if(err) throw err;
        if(result.length != 0){
            return res.send(result);
        }
        res.send('0');
    });
});

//根据id删除订单
router.get('/del_order/:id',(req,res)=>{
    let body = req.params;
    //执行sql命令
    mysql.query('delete from sh_order where oid=?',[body.id],(err,result)=>{
        if(err) throw err;
        if(result.affectedRows!=0){
            return res.send('1');
        }
        res.send('0');
    });
});

//导出路由组
module.exports = router;